import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {

    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list = new ArrayList<>();
        int n = words[0].length(), m = words.length;
        int ss = s.length();
        Map<String,Integer> hash = new HashMap<>();
        for(String str : words) hash.put(str,hash.getOrDefault(str,0) + 1);

        for(int i = 0;i < n;i++) {
        Map<String,Integer> hash1 = new HashMap<>();
        for(int left = i,right = i,count = 0;right + n <= ss;right += n) {
        String in = s.substring(right,right + n);
        hash1.put(in,hash1.getOrDefault(in,0) + 1);
        if(hash.getOrDefault(in,0) >= hash1.get(in)) count++;
        if(right - left + 1 > n*m) {
        String out = s.substring(left,left + n);
        left += n;
        if(hash.getOrDefault(out,0) >= hash1.get(out)) count--;
        hash1.put(out,hash1.get(out) - 1);
        }
        if(count == m) list.add(left);
        }
        }
        return list;
        }
        }